package Filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;
import java.io.IOException;

@WebFilter("/login/*")
public class Login_Filter implements Filter {

    public void destroy() {

    }
    public void init(FilterConfig config) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        Object LocalUser = req.getSession().getAttribute("LocalUser");
        if(req.getRequestURI().contains("Login.jsp")||req.getRequestURI().contains("register.jsp")||req.getRequestURI().contains("/Login_Check")||req.getRequestURI().contains("/Register_Check")||req.getRequestURI().contains("/UploadServlet")||LocalUser!=null||req.getRequestURI().endsWith(".css")||req.getRequestURI().endsWith(".html")
                ||req.getRequestURI().contains("userinfo.jsp")||req.getRequestURI().contains("error.jsp")||req.getRequestURI().contains("/css/")||req.getRequestURI().contains("/fonts/")||req.getRequestURI().contains("/images/")||req.getRequestURI().contains("/js/")||req.getRequestURI().contains("/login/dgut"))
        {
            chain.doFilter(req,res);
        }
        else{
            if(LocalUser==null){
                res.sendRedirect("/Login.jsp");
            }else{
                chain.doFilter(req,res);
            }
        }

    }
}